<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="30%"
    :before-close="handleClose"
  >
    <div class="class-dialog">
      <el-form
        inline
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="打款单号" prop="trade_no" style="width: 100%">
          <el-input
            placeholder="输入打款单号"
            style="width: 100%"
            v-model="ruleForm.trade_no"
          />
        </el-form-item>

        <div v-loading="loading" element-loading-text="上传中...">
          <el-form-item label="打款图片" style="width: 100%" prop="trade_pic">
            <!-- <div class="img-box" v-for="(item, i) in coverImgList" :key="i">
              <img :src="item.url" alt="" />

              <div class="occlusion">
                <el-icon class="del" @click="coverRemove(i)">
                  <IEpDelete />
                </el-icon>
              </div>
            </div> -->
            <!-- :show-file-list="false" -->
            <el-upload
              action="#"
              list-type="picture-card"
              :auto-upload="false"
              :file-list="coverImgList"
              :on-change="onChange"
              :on-remove="coverRemove"
              :on-preview="handlePictureCardPreview"
              :limit="1"
            >
              <el-icon>
                <IEpPlus />
              </el-icon>
            </el-upload>
            <el-dialog v-model="imgDialogVisible">
              <img w-full :src="imageUrl" alt="Preview Image" />
            </el-dialog>
          </el-form-item>
        </div>

        <div>
          <el-form-item label="审核意见" prop="remark" style="width: 100%">
            <el-input
              placeholder="审核意见"
              style="width: 100%"
              type="textarea"
              :row="2"
              v-model="ruleForm.remark"
            />
          </el-form-item>
        </div>

        <div>
          <el-form-item label="提现姓名" style="width: 100%">
            {{ info.bank_user }}
          </el-form-item>
        </div>
        <div>
          <el-form-item label="开户行" style="width: 100%">
            {{ info.bank_name }}
          </el-form-item>
        </div>

        <div>
          <el-form-item label="银行卡号" style="width: 100%">
            {{ info.bank_code }}
          </el-form-item>
        </div>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { tradeApi } from "@/plugins/api/shopping/withdrawal";
import { uploadImgApi } from "@/plugins/api/upload";

let ruleForm = ref({});
let rules = {
  trade_no: [{ required: true, message: "输入打款单号", trigger: "blur" }],
  trade_pic: [{ required: true, message: "打款图片", trigger: "blur" }],
};
let dialogVisible = ref(false);
function handleClose(done) {
  // dialogVisible.value = false;
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
}
let title = ref("");
let ruleFormRef = ref(null);
let info = ref({});
function initFun(row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  info.value = { ...row };
  ruleForm.value = {};
  ruleForm.value.code = row.code;
  coverImgList.value = [];
  dialogVisible.value = true;
}
// 上传图片（大）
let coverImgList = ref([]);
let loading = ref(false);
function onChange(file) {
  let formData = new FormData();
  formData.append("file", file.raw);
  loading.value = true;
  uploadImgApi(formData)
    .then((data) => {
      let obj = {
        url: data.file_url,
      };

      coverImgList.value = [];
      coverImgList.value.push(obj);

      ruleForm.value.trade_pic = data.file_url;
      loading.value = false;

      //
    })
    .catch(() => {
      loading.value = false;
    });
}

function coverRemove(index) {
  coverImgList.value.splice(index, 1);
  ruleForm.value.trade_pic = "";
}
let imgDialogVisible = ref(false);
let imageUrl = ref(null);
function handlePictureCardPreview(uploadFile) {
  imageUrl.value = uploadFile.url;
  imgDialogVisible.value = true;
}

const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      tradeApi(ruleForm.value).then((data) => {
        ElMessage.success("操作成功！");
        dialogVisible.value = false;
        emit("refreshList");
      });
    }
  });
}

onMounted(() => {});
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss">
.class-dialog {
  .img-box {
    display: inline-block;
    margin-right: 5px;
    // margin-bottom: 10px;
    width: 148px;
    position: relative;

    img {
      width: 100%;
      height: 140px;
      object-fit: cover;
    }

    .occlusion {
      display: none;
      width: 100%;
      height: 140px;
      background: #9e9c9c75;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 99;
      text-align: center;

      .del {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        line-height: 148px;
        color: #464444;
        cursor: pointer;
        font-size: large;

        &:hover {
          color: rgb(130, 131, 133);
        }
      }
    }

    &:hover {
      .occlusion {
        display: block;
      }
    }
  }
}
</style>
